﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wp8nop.Model;

namespace wp8nop.ViewModel
{
    public class DettaglioProdottoViewModel : ViewModelBase
    {
        IDataService _dataService;
        public DettaglioProdottoViewModel(IDataService dataService)
        {
            _dataService = dataService;
            Debug.WriteLine("Registro il messaggio");
            Messenger.Default.Register<NotificationMessage<ProdottoViewModel>>(this, NotificationMessageReceived);
        }

        #region PROPS

        /// <summary>
        /// The <see cref="IsLoading" /> property's name.
        /// </summary>
        public const string IsLoadingPropertyName = "IsLoading";

        private bool _isLoading = false;

        /// <summary>
        /// Sets and gets the IsLoading property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsLoading
        {
            get
            {
                return _isLoading;
            }

            set
            {
                if (_isLoading == value)
                {
                    return;
                }

                RaisePropertyChanging(IsLoadingPropertyName);
                _isLoading = value;
                RaisePropertyChanged(IsLoadingPropertyName);
            }
        }


        /// <summary>
        /// The <see cref="Prodotto" /> property's name.
        /// </summary>
        public const string ProdottoPropertyName = "Prodotto";

        private ProdottoViewModel _prodotto = null;

        /// <summary>
        /// Sets and gets the Prodotto property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ProdottoViewModel Prodotto
        {
            get
            {
                return _prodotto;
            }

            set
            {
                if (_prodotto == value)
                {
                    return;
                }

                RaisePropertyChanging(ProdottoPropertyName);
                _prodotto = value;
                RaisePropertyChanged(ProdottoPropertyName);
            }
        }

        #endregion

        private void NotificationMessageReceived(NotificationMessage<ProdottoViewModel> msg)
        {
            Debug.WriteLine("Messaggio ricevuto: " + msg.Notification);
            if (msg.Notification == "SelectedProduct")
            {
         
                Prodotto = msg.Content;
                //Debug.WriteLine("main: " + Prodotto.imgs.main);
                //Debug.WriteLine("Img: " + Prodotto.imgs.thumb);
                //Debug.WriteLine("Desc: " + Prodotto.desc.breve);
            }
        }

        internal void AddToCart()
        {
            _dataService.AddToCart(Prodotto);
        }
    }
}
